using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XNOgre;
using XNOgre.Core;
using XNOgre.Graphics;
using XNOgre.Scene;

namespace XNOgreSamples
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Browser : Microsoft.Xna.Framework.Game
    {
        SpriteBatch spriteBatch;
        Texture2D background, button;
        SpriteFont font;

        string[] samples = new string[] { "Camera Tracking", "Cel-Shading", "Character", "Cube Mapping", "Dynamic Texturing", "Fresnel", "Lighting", "Particles", "Sky", "Texture Effects/Transparency", "Water"};
        bool sampleRunning = false;
        int selectedSample = 0;
        int selectionOffset = 0;

        KeyboardState keyboard;
        float inputTimer = 0;

        string infoText = string.Empty;
        float infoTimer = 0;

        
        public Browser()
        {
            var graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Media";
            selectedSample = (samples.Length / 2);
        }

        protected override void Initialize()
        {
            Root.Instance.InitRenderSystem(GraphicsDevice);
            Root.Instance.CreateRenderWindow(Window);
            Root.Instance.Content = Content;
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            spriteBatch = new SpriteBatch(GraphicsDevice);

            font = Content.Load<SpriteFont>("Browser/Arial");
            button = Content.Load<Texture2D>("Browser/ButtonBackground");
            background = Content.Load<Texture2D>("Browser/GridBackground");
        }


        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (sampleRunning)
                return;

            keyboard = Keyboard.GetState();
            inputTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (inputTimer >= 250)
            {
                if (keyboard.IsKeyDown(Keys.Up))
                {
                    if (selectedSample - 1 >= 0)
                    {
                        selectionOffset += 45;
                        selectedSample--;
                    }
                    inputTimer = 0;
                }
                if (keyboard.IsKeyDown(Keys.Down))
                {
                    if (selectedSample + 1 < samples.Length)
                    {
                        selectionOffset -= 45;
                        selectedSample++;
                    }
                    inputTimer = 0;
                }
                if (keyboard.IsKeyDown(Keys.Enter))
                {
                    SampleSelected(samples[selectedSample]);
                    inputTimer = 0;
                }
            }
        }
        private void SampleSelected(string sample)
        {
            switch (sample)
            {
                case "Cel-Shading":
                    CelShading celSample = new CelShading(this);
                    Components.Add(celSample);
                    sampleRunning = true;
                    break;

                default:
                    infoText = sample + " Sample not yet available.";
                    break;
            }
        }
        public void StopSample()
        {
            sampleRunning = false;
        }
        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            if (sampleRunning)
                return;

            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(background, new Rectangle(0,0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);

            int maxWidth = 0;
            for (int i = 0; i < samples.Length; i++)
            {
                int width = (int)font.MeasureString(samples[i]).X;
                if (width > maxWidth)
                {
                    maxWidth = width;
                }
            }

            for (int i = 0; i < samples.Length; i++)
            {
                Color color = Color.White;
                if (i != selectedSample)
                {
                    color = Color.Silver * 0.8f;
                }

                int height = 45;
                int width = maxWidth + 20;
                int y = (i * height + 5) + 5;
                int x = 10;

                spriteBatch.Draw(button, new Rectangle(x, y + selectionOffset, width, height), color);
                spriteBatch.DrawString(font, samples[i], new Vector2(x + 20, (y + selectionOffset) + (height / 4)), Color.Black);

            }

            if (String.IsNullOrEmpty(infoText) == false)
            {
                infoTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                if (infoTimer >= 2500)
                {
                    infoText = string.Empty;
                    infoTimer = 0;
                }

                float alpha = infoTimer;
                if (alpha > 1)
                    alpha = 1;

                Color color = new Color(0, 0, 0, alpha);

                Vector2 size = font.MeasureString(infoText);
                Vector2 lowerRight = new Vector2(GraphicsDevice.Viewport.X + GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Y + GraphicsDevice.Viewport.Height);
                spriteBatch.DrawString(font, infoText, lowerRight - size, Color.Gold);

            }
            spriteBatch.End();

        }
    }
}
